package com.jzo2o.customer.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.jzo2o.common.model.CurrentUserInfo;
import com.jzo2o.common.model.PageResult;
import com.jzo2o.customer.enums.CertificationStatusEnum;
import com.jzo2o.customer.mapper.AgencyCertificationMapper;
import com.jzo2o.customer.mapper.ICertificationByAgencyMapper;
import com.jzo2o.customer.model.domain.AgencyCertification;
import com.jzo2o.customer.model.domain.AgencyCertificationRecording;
import com.jzo2o.customer.model.domain.WorkerCertification;
import com.jzo2o.customer.model.domain.WorkerCertificationRecording;
import com.jzo2o.customer.model.dto.AgencyCertificationUpdateDTO;
import com.jzo2o.customer.model.dto.request.AgencyCertificationAuditPageQueryReqDTO;
import com.jzo2o.customer.model.dto.response.AgencyCertificationAuditResDTO;
import com.jzo2o.customer.model.dto.response.WorkerCertificationAuditResDTO;
import com.jzo2o.customer.service.IAgencyCertificationService;
import com.jzo2o.mvc.utils.UserContext;
import com.jzo2o.mysql.utils.PageUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.time.LocalDateTime;

/**
 * <p>
 * 机构认证信息表 服务实现类
 * </p>
 *
 * @author itcast
 * @since 2023-09-06
 */
@Service
public class AgencyCertificationServiceImpl extends ServiceImpl<AgencyCertificationMapper, AgencyCertification> implements IAgencyCertificationService {


    @Resource
    private ICertificationByAgencyMapper certification;


    @Resource
    private AgencyCertificationMapper agencyCertificationMapper;



    /**
     * 根据机构id更新
     *
     * @param agencyCertificationUpdateDTO 机构认证更新模型
     */
    @Override
    public void updateByServeProviderId(AgencyCertificationUpdateDTO agencyCertificationUpdateDTO) {
        LambdaUpdateWrapper<AgencyCertification> updateWrapper = Wrappers.<AgencyCertification>lambdaUpdate()
                .eq(AgencyCertification::getId, agencyCertificationUpdateDTO.getId())
                .set(AgencyCertification::getCertificationStatus, agencyCertificationUpdateDTO.getCertificationStatus())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getName()), AgencyCertification::getName, agencyCertificationUpdateDTO.getName())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getIdNumber()), AgencyCertification::getIdNumber, agencyCertificationUpdateDTO.getIdNumber())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonName()), AgencyCertification::getLegalPersonName, agencyCertificationUpdateDTO.getLegalPersonName())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getLegalPersonIdCardNo()), AgencyCertification::getLegalPersonIdCardNo, agencyCertificationUpdateDTO.getLegalPersonIdCardNo())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getBusinessLicense()), AgencyCertification::getBusinessLicense, agencyCertificationUpdateDTO.getBusinessLicense())
                .set(ObjectUtil.isNotEmpty(agencyCertificationUpdateDTO.getCertificationTime()), AgencyCertification::getCertificationTime, agencyCertificationUpdateDTO.getCertificationTime());
        super.update(updateWrapper);
    }

    @Override
    public PageResult<AgencyCertificationAuditResDTO> selectByPage(AgencyCertificationAuditPageQueryReqDTO pageQueryReqDTO) {
        Page<AgencyCertificationRecording> page = PageUtils.parsePageQuery(pageQueryReqDTO, AgencyCertificationRecording.class);
        LambdaQueryWrapper<AgencyCertificationRecording> queryWrapper =
                Wrappers.<AgencyCertificationRecording>lambdaQuery().eq(ObjectUtil.isNotEmpty(pageQueryReqDTO.getAuditStatus()), AgencyCertificationRecording::getAuditStatus, pageQueryReqDTO.getAuditStatus())
                        .eq(ObjectUtil.isNotEmpty(pageQueryReqDTO.getCertificationStatus()), AgencyCertificationRecording::getCertificationStatus, pageQueryReqDTO.getCertificationStatus())
                        .like(ObjectUtil.isNotEmpty(pageQueryReqDTO.getLegalPersonName()), AgencyCertificationRecording::getLegalPersonName, pageQueryReqDTO.getLegalPersonName())
                        .like(ObjectUtil.isNotEmpty(pageQueryReqDTO.getName()),AgencyCertificationRecording::getName,pageQueryReqDTO.getName());
        Page<AgencyCertificationRecording> selectPage =certification.selectPage(page, queryWrapper);
        return PageUtils.toPage(selectPage, AgencyCertificationAuditResDTO.class);
    }


    @Transactional
    @Override
    public void updateRecording(Long id, AgencyCertificationAuditResDTO agencyCertificationResDTO) {
        CurrentUserInfo currentUserInfo = UserContext.currentUser();
        AgencyCertificationRecording recording = BeanUtil.copyProperties(agencyCertificationResDTO, AgencyCertificationRecording.class);
        recording.setAuditorId(currentUserInfo.getId());
        recording.setAuditorName(currentUserInfo.getName());
        recording.setId(id);
        recording.setAuditStatus(1);
        certification.updateById(recording);
        AgencyCertificationRecording selectById = certification.selectById(id);
        AgencyCertification agencyCertification = BeanUtil.copyProperties(selectById, AgencyCertification.class);
        agencyCertification.setId(selectById.getServeProviderId());
        agencyCertification.setCertificationTime(LocalDateTime.now());


        AgencyCertification AgencyCertification2 = new AgencyCertification();
        AgencyCertification2.setId(selectById.getServeProviderId());
        AgencyCertification2.setCertificationStatus(1);
        agencyCertificationMapper.updateById(AgencyCertification2);


        AgencyCertification AgencyCertification = agencyCertificationMapper.selectById(selectById.getServeProviderId());
        if(ObjectUtil.isNotEmpty(AgencyCertification)){
            agencyCertificationMapper.updateById(agencyCertification);
        }else {
            agencyCertificationMapper.insert(agencyCertification);
        }
    }


}
